﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")
#include "widget.h"
#include "ui_widget.h"

#include <QFileDialog>
#include <QSettings>
#include <QDebug>
#include <QMessageBox>
#include <QProcess>
#include <QXmlStreamWriter>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    init_ServiceControlParam();
    ui->serviceSettingBtn->setVisible(serviceSetting);
    connect(&m_timer,&QTimer::timeout,this,&Widget::getServerState);
    m_timer.start(1000);
}

Widget::~Widget()
{
    seri_ServiceControlParam();
    m_timer.stop();
    delete ui;
}

void Widget::getServerState()
{
    serverState = deviceOperator.getServiceState(ui->serviceName->text().toStdString().c_str());
    ui->serviceAddDelBtn->setText(serverState == -1 ? QString("安装") : QString("卸载"));
    ui->serviceStartStopBtn->setText(serverState == SERVICE_RUNNING ? QString("停止") : QString("启动"));
    ui->serviceRestartBtn->setEnabled(serverState == SERVICE_RUNNING);
    ui->serviceStartStopBtn->setEnabled(serverState != -1);
    //    ui->serviceName->setEnabled(serverState == -1);
    ui->serviceDescription->setEnabled(serverState == -1);
    switch (serverState) {
    case -1:
        ui->servoiceState->setText(QString("服务未安装！"));
        break;
    case SERVICE_RUNNING:
        ui->servoiceState->setText(QString("服务已启动！"));
        break;
    case SERVICE_STOPPED:
        ui->servoiceState->setText(QString("服务已停止！"));
        break;
    }
}

void Widget::init_ServiceControlParam()
{
    QSettings settings(QCoreApplication::applicationDirPath()+"/"+QCoreApplication::applicationName()+".ini",QSettings::IniFormat);
    settings.setIniCodec("UTF-8");

    if(settings.value("Title/name").isNull()) {
        setWindowTitle("服务管理程序");
        settings.setValue("Title/name",windowTitle());
    }else
        setWindowTitle(settings.value("Title/name").toString());

    if(settings.value("Service/serviceSetting").isNull()) {
        serviceSetting=false;
        settings.setValue("Service/serviceSetting",serviceSetting);
    }else
        serviceSetting=settings.value("Service/serviceSetting").toBool();

    if(settings.value("Service/serviceSettingPath").isNull()) {
        serviceSettingPath="";
        settings.setValue("Service/serviceSettingPath",serviceSettingPath);
    }else
        serviceSettingPath=settings.value("Service/serviceSettingPath").toString();

    if(settings.value("Service/serverName").isNull()) {
        ui->serviceName->setText("MyService");
        settings.setValue("Service/serverName",ui->serviceName->text());
    }else
        ui->serviceName->setText(settings.value("Service/serverName").toString());

    if(settings.value("Service/serverDescription").isNull()) {
        ui->serviceDescription->setText("我的服务");
        settings.setValue("Service/serverDescription",ui->serviceDescription->text());
    }else
        ui->serviceDescription->setText(settings.value("Service/serverDescription").toString());

    if(settings.value("Service/serverPath").isNull()) {
        servicePath="";
        settings.setValue("Service/serverPath",servicePath);
    }else
        servicePath=settings.value("Service/serverPath").toString();

    if(settings.value("Service/WinSWPath").isNull()) {
        WinSWPath="";
        settings.setValue("Service/WinSWPath",WinSWPath);
    }else
        WinSWPath=settings.value("Service/WinSWPath").toString();

    if(settings.value("Service/WinSWConfigPath").isNull()) {
        WinSWConfigPath="";
        settings.setValue("Service/WinSWConfigPath",WinSWConfigPath);
    }else
        WinSWConfigPath=settings.value("Service/WinSWConfigPath").toString();


    if(settings.value("WinSW/id").isNull()) {
        id="";
        settings.setValue("WinSW/id",id);
    }else
        id=settings.value("WinSW/id").toString();

    if(settings.value("WinSW/logPath").isNull()) {
        logPath="logs";
        settings.setValue("WinSW/logPath",logPath);
    }else
        logPath=settings.value("WinSW/logPath").toString();

    if(settings.value("WinSW/logMode").isNull()) {
        logMode="roll";
        settings.setValue("WinSW/logMode",logMode);
    }else
        logMode=settings.value("WinSW/logMode").toString();

//    if(settings.value("WinSW/executable").isNull()) {
//        executable="";
//        settings.setValue("WinSW/executable",executable);
//    }else
//        executable=settings.value("WinSW/executable").toString();

    if(settings.value("WinSW/startArguments").isNull()) {
        startArguments="";
        settings.setValue("WinSW/startArguments",startArguments);
    }else
        startArguments=settings.value("WinSW/startArguments").toString();

    if(settings.value("WinSW/stopArguments").isNull()) {
        stopArguments="-s stop";
        settings.setValue("WinSW/stopArguments",stopArguments);
    }else
        stopArguments=settings.value("WinSW/stopArguments").toString();
}

void Widget::seri_ServiceControlParam()
{
    qDebug() << QCoreApplication::applicationDirPath()+"/"+QCoreApplication::applicationName()+".ini";
    QSettings settings(QCoreApplication::applicationDirPath()+"/"+QCoreApplication::applicationName()+".ini",QSettings::IniFormat);
    settings.setIniCodec("UTF-8");

    settings.setValue("Service/serverName",ui->serviceName->text());
    settings.setValue("Service/serverDescription",ui->serviceDescription->text());
}

bool Widget::writeWinSWParam()
{
    QFile file(WinSWConfigPath);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QMessageBox::information(this,"温馨提示",QString("WinSW程序配置文件打开失败%1").arg(WinSWConfigPath));
        return false;
    }

    QXmlStreamWriter xmlWriter(&file);
    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();
    xmlWriter.writeStartElement("service");

    xmlWriter.writeTextElement("id", id);
    xmlWriter.writeTextElement("name", ui->serviceName->text());
    xmlWriter.writeTextElement("description", ui->serviceDescription->text());
    xmlWriter.writeTextElement("logpath", QCoreApplication::applicationDirPath()+"/"+logPath);
    xmlWriter.writeTextElement("logmode", logMode);
    xmlWriter.writeTextElement("executable", QCoreApplication::applicationDirPath()+"/"+servicePath);
    xmlWriter.writeTextElement("startarguments", startArguments);
    xmlWriter.writeTextElement("stoparguments", stopArguments);

    xmlWriter.writeEndElement(); // service
    xmlWriter.writeEndDocument();

    file.close();
    return true;
}

void Widget::on_serviceAddDelBtn_clicked()
{
    QString exePath = QString("%1/%2").arg(QCoreApplication::applicationDirPath()).arg(WinSWPath);
    if(!writeWinSWParam()) return;
    QString serverName;
    serverName = ui->serviceName->text();
    if(serverState == -1){
        if(!serverName.isEmpty() && !servicePath.isEmpty()){
            QProcess::startDetached(exePath,QStringList{"install"});
        }else{
            QMessageBox::information(this,"温馨提示","服务名称和服务路径不可为空！");
        }
    }else{
        QProcess::startDetached(exePath,QStringList{"uninstall"});
    }
}

void Widget::on_serviceStartStopBtn_clicked()
{
    if(serverState == SERVICE_STOPPED){
        deviceOperator.StartServiceInterface(ui->serviceName->text().toStdString().c_str());
    }else if(serverState == SERVICE_RUNNING){
        deviceOperator.StopServiceInterface(ui->serviceName->text().toStdString().c_str());
    }
}

void Widget::on_serviceRestartBtn_clicked()
{
    deviceOperator.StopServiceInterface(ui->serviceName->text().toStdString().c_str());
    deviceOperator.StartServiceInterface(ui->serviceName->text().toStdString().c_str());
}

void Widget::on_serviceSettingBtn_clicked()
{
    QString path = QString("%1/%2").arg(QCoreApplication::applicationDirPath()).arg(serviceSettingPath);
    QProcess *process = new QProcess(this);
    process->start(path);
}
